<?php
    require_once 'header.php';
   	require_once 'config.php';
	require_once 'Date.php';

    if(!empty($_REQUEST["month"])) {
	    $m = (int)$_REQUEST["month"];
    }
    else {
        $m = date('m');
    }
	if(!empty($_REQUEST["year"])) {
	    $y = (int)$_REQUEST["year"];
    }
    else {
        $y = date('Y');
    }

	if(!empty($_REQUEST["date"])) {
	    $d = (int)$_REQUEST["date"];
    }
    else {
        $d = date('d');
    }


    $expdate =$year."-".$month."-".$day;
    $dtstr = $y . "-" . sprintf("%02d", $m) . "-" . sprintf("%02d", $d);
    $dt =& new Date($dtstr);

    $nextdt = $dt->getNextDay();
    $prevdt = $dt->getPrevDay();


    $rs=getCategory();
	$rcredit = getCreditCards($_SESSION["userid"]);

	// DELETE EXPENSE
	if( isset($_REQUEST["action"]) && ($_REQUEST["action"]== 'DelExp'))
	{
		$expid= $_REQUEST["expid"];
		delExpense($expid,(int)$_SESSION["userid"]);
	}

	// UPDATE EXPENSE
	else if(isset($_REQUEST["action"]) && $_REQUEST["action"] == "updateexp") {
	    UpdateExpense($_REQUEST["expid"],$_REQUEST["expamt"],$_REQUEST["expdate"],
	        	$_REQUEST["expdesc"],$_REQUEST["expcat"], (int)$_SESSION["userid"],
	        	$_REQUEST["trantype"]);
	}


    else if(isset($_REQUEST["expamt"]) && isset($_REQUEST["expdesc"]) &&isset($_REQUEST["expcat"]))
    {
        addExpense($_REQUEST["expamt"],$dtstr,$_REQUEST["expdesc"],$_REQUEST["expcat"], (int)$_SESSION["userid"],$_REQUEST["trantype"]);
    }

    $alldata = getExpforDate($y,$m,$d,(int)$_SESSION["userid"]);

    $yeardata = getYeardata($y, $_SESSION['userid']);
    $formaty = sprintf("%.2f", $yeardata);

    $monthdata = getMonthdata($m,$d,$y);
    $formatm =  sprintf("%.2f", $monthdata);

    $weekdata = getWeekdata($m,$d,$y, $_SESSION['userid']);
    $formatw =  sprintf("%.2f", $weekdata);
?>
<script language="javascript" content="text/plain">

function confirmExpDel(id)
{
	//alert("here " + id);
  if (confirm("Are you sure you want to delete?"))
  	{
  		document.getElementById("idaction").value='DelExp';
  		document.getElementById("idexpid").value=id;
  		document.addexp.submit();
  	}
}

function UpdateExp(id)
{
  		document.getElementById("idaction").value='DelExp';
  		document.getElementById("idexpid").value=id;
  		document.addexp.action = 'update.php';
  		document.addexp.submit();

        //window.location = "update.php?id="+id;
}

function addExp() {
	if(document.addexp.expdesc.value == '') {
		alert("Please add expense description.");
		document.addexp.expdesc.focus();
		return false;
	}
	else if(document.addexp.expamt.value == '') {
		alert("Please add expense amount.");
		document.addexp.expamt.focus();
		return false;
	}	document.addexp.submit();
}

function showAddNew() {
	document.getElementById("expadd_tbl").style.display = "block";
	document.getElementById("hidenew_link").style.display = "block";
	document.getElementById("addnew_link").style.display = "none";
}
function hideAddNew() {
	document.getElementById("expadd_tbl").style.display = "none";
	document.getElementById("hidenew_link").style.display = "none";
	document.getElementById("addnew_link").style.display = "block";
}
</script>
<br/><br/>
<table style="width: 100%; border: 0px;" class="menu_table">
<tr>
<td colspan="3" style="text-align: center;"><?=ucwords($_SESSION['username']);?>'s Expenses <?= $dt->getMonthName() ?> <?= $dt->getDay() ?>, <?= $y ?></td>
</tr>
<tr>
<td style="text-align: left; width: 10%;"><a href="<?= $_SERVER["PHP_SELF"] ?>?year=<?= $prevdt->getYear() ?>&month=<?= $prevdt->getMonth() ?>&date=<?= $prevdt->getDay() ?>" title="View yesterday's expense">&lt;&lt;</a></td>
<td style="text-align: center; width: 80%;"><a href="table.php?year=<?= $y ?>&month=<?= $m ?>" title="Back to Month View"><?= $dt->getMonthName() ?> <?= $dt->getYear() ?> at a Glance</td>
<td style="text-align: right; width: 10%;"><a href="<?= $_SERVER["PHP_SELF"] ?>?year=<?= $nextdt->getYear() ?>&month=<?= $nextdt->getMonth() ?>&date=<?= $nextdt->getDay() ?>" title="View tomorrow's expense">&gt;&gt;</a></td>
</tr>
</table>
    <br/><br/>
      <table cellspacing="0" cellpadding="0" style="width: 100%; border: 1px solid rgb(200,200,200);">
       <tr>
	    <td><strong>Expense This Year:<strong>
		<a href="showgraph.php?type=cat&duration=yearly&year=<?= $y ?>&month=<?= $m ?>&date=<?= $d ?>" title="Graphical View">$<?=$formaty?></a>
	    </td>
	    <td><strong>Expense This Month:<strong>
                <a href="showgraph.php?type=cat&duration=monthly&year=<?= $y ?>&month=<?= $m ?>&date=<?= $d ?>" title="Graphical View">$<?=$formatm?></a>
            </td>
	    <td>Expense This Week:<strong>
		<a href="showgraph.php?type=cat&duration=weekly&year=<?= $y ?>&month=<?= $m ?>&date=<?= $d ?>" title="Graphical View">$<?=$formatw?></a>
 	    </td>
       </tr>
       </table>

       <br/>
<h4 style="text-align: left;">Expenses Today</h4>
       <form name="addexp" action="index.php" method="post">
       <input type="hidden" id="idyear" name="year" value="<?= $y ?>" />
       <input type="hidden" id="idmonth" name="month" value="<?= $m ?>" />
       <input type="hidden" id="iddate" name="date" value="<?= $d ?>" />
       <input type="hidden" id="idaction" name="action" value="" />
       <input type="hidden" id="idexpid" name="expid" value="" />
<div style="text-align: left; background-color: #cfecec;" id="addnew_link">
	<a style="text-align: left;" href="javascript: void(0);" onclick="showAddNew();" title="Add New Expense">[+]</a> Add New
</div>
<div style="text-align: left; display: none; background-color: #cfecec;" id="hidenew_link">
	<a style="text-align: left;" href="javascript: void(0);" onclick="hideAddNew();" title="Hide New Expense">[+]</a> Hide
</div>
<div style="text-align: center; display: none; background-color: #cfecec; padding: 5px;" id="expadd_tbl">
<b>Add An Expense</b>
       <table cellspacing="0" cellpadding="0" align="center" style="border: 1px solid rgb(200,200,200);">
        <tr>
		<th>Expense Category</th>
       <td>
            <select name="expcat" id="idselcategory" style="width: 250px;">
<?php
    if(is_array($rs)) {
	 foreach($rs as $row) {
?>
             <option value="<?=$row["category_id"]?>"><?=$row["category_name"]?></option>
<?php
	 }
    }
?>

            </select>
       </td>
</tr>
<tr>
	   <th>Expense Description</th>
       <td><input type="text" name="expdesc" id="idexpdesc" maxlength="100" style="width: 250px;"/></td>
</tr>
<tr>
            <th>Amount</th>
       <td>$ <input type="text" name="expamt" id="idexpamt" maxlength="6" style="width: 238px;"/></td>
</tr>
<tr>
            <th>Cash/Card?</th>
       <td>
   			<select name="trantype" id="idtrantype" style="width: 250px;">
<?php
    if(is_array($rcredit)) {
	    foreach($rcredit as $row) {
?>
            <option value="<?=$row["cc_id"]?>"><?=$row["cc_nickname"]?></option>
<?php
	    }
    }
?>
            </select>

       </td>
</tr>
        <tr>
        <td align="center" colspan="2"><input type="button" name="expadd" id="idexpadd" value="Save" onclick="addExp();"/></td>
        </tr>
	</table>
</div>
        </form>


       <table cellspacing="2" cellpadding="2" style="width: 100%; border: 0px solid rgb(200,200,200);">
        <tbody id="expenses">
        <tr>
		<th>&nbsp;</th>
            <th>Expense Category</th>
            <th>Expense Description</th>
            <th>Transaction Type</th>
            <th>Amount ($)</th>
            <th colspan="2">Actions</th>
       </tr>
<?php
    if(is_array($alldata) && !empty($alldata)) {
	    $i = 0;
	    foreach($alldata as $data) {
?>
	<tr style="background-color : <?=$data["category_color"]?> ">
            <td><?= ++$i ?></td>
            <td><?=$data["Category_name"]?></td>
            <td><?=$data["exp_desc"]?></td>
            <td><?=$data["cc_nickname"]?></td>
            <td><?=$data["exp_amt"] ?></td>
            <td><a href="javascript:void(0);" onclick="UpdateExp(<?=$data['exp_id']?>);">Edit</a></td>
            <td><a href="javascript:void(0);" onclick="confirmExpDel(<?=$data['exp_id']?>);">Delete</a></td>
<?php
	    }
    }
    else {
	    echo '<tr><td colspan="6">No expenses found.</td></tr>';
    }
?>

        </tr>
        </tbody>
    </table>
</body>
</html>
